package sjtu.ist.warehouse4ws.action;

import java.io.IOException;
import java.io.StringBufferInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.ServletActionContext;
import org.xml.sax.SAXException;


import sjtu.ist.warehouse4ws.epc2bpmn.GenerateBpmn;
import sjtu.ist.warehouse4ws.epc2bpmn.ParserEpc;
import sjtu.ist.warehouse4ws.jcr.RepositoryMoudl;
import sjtu.ist.warehouse4ws.tool.MyUtils;




import com.opensymphony.xwork2.ActionSupport;



public class GenerateBpmnAction extends ActionSupport{
	/**
	 * 
	 */
	private static final long serialVersionUID = -3335592226930869947L;
	private GenerateBpmn generateBpmn;
	private String nodeName;
	private RepositoryMoudl repositoryMoudlService;
	    
		
		public RepositoryMoudl getRepositoryMoudlService() {
			return repositoryMoudlService;
		}

		public void setRepositoryMoudlService(RepositoryMoudl repositoryMoudlService) {
			this.repositoryMoudlService = repositoryMoudlService;
		}

	public String getNodeName() {
		return nodeName;
	}
	public void setNodeName(String nodeName) {
		this.nodeName = nodeName;
	}
	public void setGenerateBpmn(GenerateBpmn generateBpmn) {
		this.generateBpmn = generateBpmn;
	}
	public String generateBpmn(){

		MyUtils.mkDirectory(ServletActionContext.getServletContext().getRealPath("/upload/files/bpmn"));
		String outpath=ServletActionContext.getServletContext().getRealPath("/upload/files/bpmn/").replaceAll("\\\\", "/")+"/";


		Subject currentUser = SecurityUtils.getSubject();
		String companyId=currentUser.getSession().getAttribute("companyId").toString();	

		HttpServletRequest request = ServletActionContext.getRequest();
		String xmlName = request.getParameter("nodeName");
		String xmlVersion=request.getParameter("nodeVersion");
	   
		
		List<String> addFile=new ArrayList<String>();
	    HashMap <String,String>fs=this.repositoryMoudlService.getAllFile(companyId, xmlName, xmlVersion,"process");
			generateBpmn.setFs(fs);
		    Iterator it = fs.keySet().iterator();
			while (it.hasNext()) {
				String key = (String) it.next();
				String outFile=outpath+"BPMN_"+key;
			    try {
					generateBpmn.writeBpmn(outFile, ParserEpc.parseByIS(new StringBufferInputStream(fs.get(key))));
					addFile.add(outFile);
				} catch (SAXException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ParserConfigurationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}	
			}
		this.repositoryMoudlService.addServiceFile(companyId, xmlName, xmlVersion, "bpmn", addFile);
		return SUCCESS;
	}
}
